#include "io_transfer.h"

/*
 * Handle an I/O request.
 */
void phs_transfer(struct request *req, struct phs_device *dev, sector_t sector, unsigned long nsect, int write) {
        unsigned long offset = sector * LOGICAL_BLOCK_SIZE;
        unsigned long nbytes = nsect * LOGICAL_BLOCK_SIZE;
	unsigned long flags;

	spin_unlock_irq(req->q->queue_lock);

        if ((offset + nbytes) > dev->size) {
                printk (KERN_NOTICE "phs: Beyond-end write (%ld %ld)\n", offset, nbytes);
                return;
        }
        if (write){
                memcpy(dev->data + offset, req->buffer, nbytes);
	        // docasne kvoli testovaniu
		spin_lock_irqsave(req->q->queue_lock, flags);
        	__blk_end_request_all(req, 0);
        	spin_unlock_irqrestore(req->q->queue_lock, flags);

//              send_swap_out_request(req, offset, nbytes);
        }else
                memcpy(req->buffer, dev->data + offset, nbytes);
		// docasne kvoli testovaniu
	        spin_lock_irqsave(req->q->queue_lock, flags);
        	__blk_end_request_all(req, 0);
        	spin_unlock_irqrestore(req->q->queue_lock, flags);

//              send_swap_in_request(req, offset, nbytes);

	spin_lock_irq(req->q->queue_lock);
	return;
}

int phs_read_data(char* buffer, u32 *pozicia, u32 len) {
	/* je to sice strasne zranitelne napisane,
	   kedze nie je ziadne overenie ci data na tej
	   pozicii naozaj su, ale povedzme :)
	*/
	memcpy(buffer, pozicia, len);            
	return 1;
}



int phs_save_data(char *buffer, u32 len, u32 *smernik) {

	smernik = NULL;
	smernik = vmalloc(len);

	// ak sa nam allokovanie pamati dosere ...
	if (smernik == NULL) {
		printk (KERN_NOTICE "phs-save_data: neni priestor pre ulozenie dat\n");
		return -ENOMEM;
	}
	memcpy(smernik, buffer, len);
	return 1;
}


int phs_ack_read_request(struct request *req, int ackBit, char *buffer, u32 nBytes) {
	// ked sme nacitali data tak mu ich vratime, cize do requestu zapiseme nacitane data
	int error = (ackBit < 0) ? -EIO : 0;
        unsigned long flags;

// ked bude peto fungovat tak odkomentujem VK
//
//	if (ackBit == 0) memcpy(req->buffer, buffer, nBytes);
//        spin_lock_irqsave(req->q->queue_lock, flags);
//        __blk_end_request_all(req, error);
//        spin_unlock_irqrestore(req->q->queue_lock, flags);

	//if ( blk_end_request_all(req, ackBit)) {
        //        printk (KERN_NOTICE "phs-req_read: Request zlyhal\n");
        //}
	printk(KERN_INFO ": DBG: vladova fcia phs_ack_read_request\n");
	phs_debug(DBG_IP, "DBG: vladova fcia phs_ack_read_request");
	return 1;
}



int phs_ack_write_request(struct request *req, int ackBit) {
	// ked data mame zapisane tak mu vratime len potvrdenie
	int error = (ackBit < 0) ? -EIO : 0;
	unsigned long flags;
	
	printk(KERN_INFO ": DBG: vladova fcia phs_ack_write_request\n");
	phs_debug(DBG_IP, "DBG: vladova fcia phs_ack_write_request");
	printk(KERN_INFO": DBG: phs_ack_write_request start\n");

// ked bude peto fungovat tak odkomentujem VK
//
//	spin_lock_irqsave(req->q->queue_lock, flags);
//	__blk_end_request_all(req, error);
//	spin_unlock_irqrestore(req->q->queue_lock, flags);

//	if ( blk_end_request_all(req, ackBit)) {
//		printk (KERN_NOTICE "phs-req_write: Request zlyhal\n");
//        }
	return 1;
}
